package cn.zyjw.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.zyjw.config.IdAutoConfiguration;
import cn.zyjw.domain.User;
import cn.zyjw.domain.UserAuthAuditRecord;
import cn.zyjw.domain.UserAuthInfo;
import cn.zyjw.geet.GeetestLib;
import cn.zyjw.mapper.UserMapper;
import cn.zyjw.model.IdCardForm;
import cn.zyjw.service.UserAuthAuditRecordService;
import cn.zyjw.service.UserAuthInfoService;
import cn.zyjw.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private GeetestLib geetestLib;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private UserAuthAuditRecordService userAuthAuditRecordService;

    @Autowired
    private Snowflake snowflake;

    @Autowired
    private UserAuthInfoService userAuthInfoService;

    /**
     * 身份认证
     *
     * @param userId
     * @param idCardForm
     * @return
     */
    @Override
    public Boolean authAccount(Long userId, IdCardForm idCardForm) {
        //获取用户
        User user = getById(userId);
        Assert.notNull(user, "用户不存在");
        if (user.getAuthStatus() != 0) {
            throw new IllegalArgumentException("该用户已经认证成功了");
        }
        //检验
        idCardForm.check(geetestLib, redisTemplate);
        //认证
        boolean checkIdcard = IdAutoConfiguration.checkIdcard(idCardForm.getIdCard(), idCardForm.getRealName());
        if (!checkIdcard) {
            throw new IllegalArgumentException("该用户信息错误,请检查");
        }
        //修改用户状态
        // 设置用户的认证属性
        user.setAuthtime(new Date());
        user.setAuthStatus((byte) 1);
        user.setRealName(idCardForm.getRealName());
        user.setIdCard(idCardForm.getIdCard());
        user.setIdCardType(idCardForm.getIdCardType());

        return updateById(user);
    }

    /**
     * 高级认证
     *
     *
     * @param userId
     * @param imgs
     * @return
     */
    @Override
    @Transactional
    public Boolean authUser(Long userId, String[] imgs) {
        User user = getById(userId);
        long authCode = snowflake.nextId();
        Assert.notNull(user,"用户不存在");
        List<UserAuthInfo> userAuthInfoList = new ArrayList<>(imgs.length);
        for (int i = 0; i < imgs.length; i++) {
            String s = imgs[i];
            UserAuthInfo userAuthInfo = new UserAuthInfo();
            userAuthInfo.setImageUrl(imgs[i]);
            userAuthInfo.setUserId(userId);
            userAuthInfo.setSerialno(i + 1);  // 设置序号 ,1 正面  2 反面 3 手持
            userAuthInfo.setAuthCode(authCode); // 是一组身份信息的标识 3 个图片为一组
            userAuthInfoList.add(userAuthInfo);
        }
        userAuthInfoService.saveBatch(userAuthInfoList);
        user.setReviewsStatus(0); // 等待审核
        return updateById(user);
    }

    @Override
    public User getById(Serializable id) {
        User user = super.getById(id);
        if(user==null){
            throw new IllegalArgumentException("请输入正确的用户Id") ;
        }
        Byte seniorAuthStatus = null ; // 用户的高级认证状态
        String seniorAuthDesc = "" ;// 用户的高级认证未通过,原因
        Integer reviewsStatus = user.getReviewsStatus(); // 用户被审核的状态 1通过,2拒绝,0,待审核"
        if(reviewsStatus == null){ //为null 时,代表用户的资料没有上传
            seniorAuthStatus = 3 ;
            seniorAuthDesc = "资料未填写" ;
        }else {
            switch (reviewsStatus) {
                case 1:
                    seniorAuthStatus = 1;
                    seniorAuthDesc = "审核通过";
                    break;
                case 2:
                    seniorAuthStatus = 2;
                    // 查询被拒绝的原因--->审核记录里面的
                    // 时间排序,
                    List<UserAuthAuditRecord> userAuthAuditRecordList = userAuthAuditRecordService.getUserAuthAuditRecordList(user.getId());
                    if (!CollectionUtils.isEmpty(userAuthAuditRecordList)) {
                        UserAuthAuditRecord userAuthAuditRecord = userAuthAuditRecordList.get(0);
                        seniorAuthDesc = userAuthAuditRecord.getRemark();
                    }
                    seniorAuthDesc = "原因未知";
                    break;
                case 0:
                    seniorAuthStatus = 0;
                    seniorAuthDesc = "等待审核";
                    break;

            }
        }
        user.setSeniorAuthStatus(seniorAuthStatus);
        user.setSeniorAuthDesc(seniorAuthDesc);
        return user;
    }

}
